<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- cell -->
    <input ref="inp" type="file" hidden @change="onInputChange" />
    <van-cell title="头像" is-link @click="$refs.inp.click()">
      <van-image class="avatar" fit="cover" round :src="userInfo.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="userInfo.name"
      is-link
      @click="isEditName = true"
    />
    <van-cell
      title="性别"
      :value="userInfo.gender ? '女' : '男'"
      is-link
      @click="isEditGender = true"
    />
    <van-cell
      title="生日"
      :value="userInfo.birthday"
      is-link
      @click="isEditBirthday = true"
    />
    <!-- 修改昵称弹出层 -->
    <van-popup
      v-model="isEditName"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <UpdateName
        v-if="isEditName"
        @close="isEditName = false"
        v-model="userInfo.name"
      />
    </van-popup>
    <!-- 修改性别弹出层 -->
    <van-popup v-model="isEditGender" position="bottom">
      <UpdateGender
        v-if="isEditGender"
        @close="isEditGender = false"
        v-model="userInfo.gender"
      />
    </van-popup>
    <!-- 修改生日弹出层 -->
    <van-popup v-model="isEditBirthday" position="bottom">
      <UpdateBirthday
        v-if="isEditBirthday"
        @close="isEditBirthday = false"
        v-model="userInfo.birthday"
      />
    </van-popup>
    <!-- 修改头像弹出层 -->
    <van-popup
      v-model="isEditAvatar"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <UpdateAvatar
        :img="imgUrl"
        @close="isEditAvatar = false"
        v-model="userInfo.photo"
        v-if="isEditAvatar"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user.js'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/updete-gender.vue'
import UpdateBirthday from './components//update-birthday.vue'
import UpdateAvatar from './components/update-avatar.vue'
export default {
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdateAvatar
  },
  data () {
    return {
      // 个人资料
      userInfo: {},
      // 昵称弹出层
      isEditName: false,
      isEditGender: false,
      isEditBirthday: false,
      imgUrl: '',
      isEditAvatar: false
    }
  },
  created () {
    this.loadUserInfo()
  },
  methods: {
    // 获取用户资料
    async loadUserInfo () {
      try {
        const { data: { data: res } } = await getUserProfile()
        console.log(res)
        this.userInfo = res
      } catch (err) {
        this.$toast('加载资料失败')
      }
    },
    // 触发file input
    onInputChange (e) {
      const file = e.target.files[0]
      console.log(file)
      this.imgUrl = URL.createObjectURL(file)
      this.isEditAvatar = true
      e.target.value = ''
    }
  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
